package com.comac.print.component.app.controller;

import cc.comac.common.sso.util.CookieUtil;
import cc.comac.techfoundation.audit.annotation.OptLog;
import cc.comac.techfoundation.audit.enums.OptType;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.comac.print.component.app.domain.PrintMasterplate;
import com.comac.print.component.app.domain.dto.PrintMasterplateSearchDTO;
import com.comac.print.component.app.page.PageResponse;
import com.comac.print.component.app.service.PrintMasterplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 模板信息 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2025-04-16 03:51:14
 */
@Api(tags = "PrintMasterplateController管理功能控制器")
@RestController
@RequestMapping("/manage/template")
@Slf4j
//@FeignClient(value = "tech-foundation", name="tech-foundation", path = "/tech-foundation")
public class PrintMasterplateController {


    @Autowired
    private PrintMasterplateService printMasterplateService;

    /**
     * 分页查询模板信息
     *
     * @param searchDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    @ApiOperation("分页查询模板信息")
    PageResponse<PrintMasterplate> page(@RequestBody PrintMasterplateSearchDTO searchDTO) {
        return printMasterplateService.searchPage(searchDTO);
    }

    ;

    /**
     * 添加单个模板信息
     *
     * @param entity 模板信息实体
     * @return 添加结果
     */
    @PostMapping("")
    @ApiOperation("添加单个模板信息")
    @OptLog(optType = OptType.INSERT, detail = "添加单个模板,模板名称：{#entity.systemId}")
    Response add(@RequestBody PrintMasterplate entity) {

        boolean result = printMasterplateService.addPrintMasterplate(entity);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "添加单个模板信息失败");
    }

    /**
     * 升版单个模板信息
     *
     * @param entity 模板信息实体
     * @return 添加结果
     */
    @PostMapping("/upgrade")
    @ApiOperation("升版模板")
    @OptLog(optType = OptType.UPDATE, detail = "升版模板,模板名称：{#entity.systemId}")
    Response upgrade(@RequestBody PrintMasterplate entity) {

        boolean result = printMasterplateService.upgradePrintMasterplate(entity);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "升版模板失败");
    }


    /**
     * 删除单个模板信息
     *
     * @param id 模板信息ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除单个模板信息")
    @OptLog(optType = OptType.DELETE, detail = "删除单个模板,模板id：{#id}")
    Response delete(@PathVariable Long id) {
        boolean result = printMasterplateService.removeById(id);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "删除单个模板信息失败");
    }

    /**
     * 批量删除模板信息
     *
     * @param ids 模板信息ID列表
     * @return 批量删除结果
     */
    @DeleteMapping("/delete")
    @ApiOperation("批量删除模板信息")
    Response deleteBatch(@RequestBody List<Long> ids) {
        boolean result = printMasterplateService.removeByIds(ids);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "批量删除模板信息失败");
    }

    /**
     * 更新单个模板信息
     *
     * @param entity 模板信息实体
     * @return 更新结果
     */
    @PutMapping("")
    @ApiOperation("更新单个模板信息")
    @OptLog(optType = OptType.UPDATE, detail = "更新模板,模板id：{#id}")
    Response update(@RequestBody PrintMasterplate entity) {
        boolean result = printMasterplateService.updateById(entity);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "更新单个模板信息失败");
    }


    /**
     * 根据ID查询模板信息详情
     *
     * @param id 模板信息ID
     * @return 模板信息实体
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询模板信息详情")
//    @OptLog(optType = OptType.QUERY, detail = "根据ID查询模板信息详情,id：{#id}")
    SingleResponse<PrintMasterplate> findById(@PathVariable Long id) {
        return SingleResponse.of(printMasterplateService.getById(id));
    }

    /**
     * 根据systemId + bussinessId查询模板信息详情
     *
     * @param systemId 模板信息ID
     * @param bussinessId 模板信息ID
     * @return 模板信息实体List
     */
    @GetMapping("/getHistory")
    @ApiOperation("systemId + bussinessId查询模板信息详情")
    SingleResponse<List<PrintMasterplate>> getHistory(@RequestParam("systemId") String systemId,
                                                      @RequestParam("bussinessId") String bussinessId) {
        PrintMasterplate entity = new PrintMasterplate();
        entity.setSystemId(systemId);
        entity.setBussinessId(bussinessId);
        return SingleResponse.of(printMasterplateService.getHistoryPrintMasterplate(entity));
    }

    /**
     * 模板信息启用/停用
     *
     * @param entity 模板信息对象
     * @return 更新状态结果
     */
//    @PutMapping("/changeStatus")
//    @ApiOperation("模板信息启用/停用，状态（0正常 1停用），传id和status")
//    Response changeStatus(@RequestBody PrintMasterplate entity){
//        boolean result = printMasterplateService.updateById(entity);
//        return result ? Response.buildSuccess() : Response.buildFailure("-1", "模板信息启用/停用失败");
//    }


}